﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using BLL;

namespace DAL
{
    public class ServiceStatusText : IStatusTextService
    {
        private DbHelper DbHelper;
        public ServiceStatusText()
        {
            DbHelper = new DbHelper();
        }
       

        /// <summary>
        /// 根据ID返回
        /// </summary>
        public StatusText GetById(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 * from StatusText ");
            strSql.Append(" where StatusId=@StatusId ");
            SqlParameter[] parameters = {
					new SqlParameter("@StatusId", SqlDbType.Int,4)};
            parameters[0].Value = Id;

            using (DataTable table = DbHelper.GetDataTable(strSql.ToString(), parameters))
            {
                StatusText model = new StatusText();

                if (table.Rows.Count > 0)
                {
                    if (table.Rows[0]["StatusId"].ToString() != "")
                    {
                        model.StatusId = int.Parse(table.Rows[0]["StatusId"].ToString());
                    }
                    model.StatusName = table.Rows[0]["StatusName"].ToString();
                    return model;
                }
                else
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// 返回根据条件
        /// </summary>
        private IList<BLL.StatusText> GetBySql(string strSql)
        //public  IList<BLL.StatusText> GetBySql(string strSql)
        {
            using (DataTable table = DbHelper.GetDataTable(strSql))
            {
                IList<StatusText> list = new List<StatusText>();
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    StatusText model = new StatusText();
                    if (table.Columns.Contains("StatusId") && table.Rows[i]["StatusId"] != DBNull.Value)
                        model.StatusId = int.Parse(table.Rows[i]["StatusId"].ToString());
                    if (table.Columns.Contains("StatusName") && table.Rows[i]["StatusName"] != DBNull.Value)
                        model.StatusName = table.Rows[i]["StatusName"].ToString();
                    list.Add(model);
                }
                return list;
            }
        }

        /// <summary>
        /// 返回全部
        /// </summary>
        public IList<StatusText> GetAll()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM StatusText ");
            strSql.Append(" order by  StatusId");
            return GetBySql(strSql.ToString());
        }

        //void IStatusTextService.GetAll()
        //{
        //    throw new NotImplementedException();
        //}
    }
}
